﻿$(function() {
    autoResize({ dataGrid: '#Grid', callback: Initlist });
    window_edit = $('#Add').window({
        iconCls: 'icon-add',
        zIndex: 1000,
        width: 360,
        height: 300,
        draggable: true,
        resizable: false,
        shadow: true,
        inline: true,
        modal: true,
        title: '添加字典列表',
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closable: true,
        closed: true,
        onOpen: function() {
            $('#Add').parent().css("left", "25%");
            $('#Add').parent().next().css("left", "25%");
            $('#Add').parent().next().css("top", "25%");
            $('#Add').parent().css("top", "25%");
        }
    });
    $('#Add').show();
    window_edit_form = $("#Add").find("form");

    window_type = $('#DictionaryType').window({
        iconCls: 'icon-add',
        zIndex: 1000,
        width: 800,
        height: 600,
        draggable: true,
        resizable: false,
        shadow: true,
        inline: true,
        modal: true,
        title: '添加字典类型',
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closable: true,
        closed: true

    });
    comboboxInit();
});

function comboboxInit() {
    cmd_type = $("#TypeId").combobox({
        url: "../Handler/AjaxComboboxData.ashx",
        valueField: 'id',
        textField: 'text'
    }).combobox({ onSelect: function(record) {
        cmd_parent = $("#ParentId").combobox({
            url: "../Handler/AjaxComboboxData.ashx?typeId=" + record.id,
            valueField: "id",
            textField: "text"
        });
    }
    });


}

function Initlist(size) {
    param.Method = "Paged";
    grid = $('#Grid').datagrid({
        tilte: '字典列表信息',
        iconCls: 'icon-save',
        width: size.width,
        height: size.height,
        pageSize: Math.round(size.height / 25),
        pageList: [Math.round(size.height / 25)],
        nowrap: true,
        striped: true,
        url: '../Handler/AjaxHandle.ashx',
        queryParams: { "json": $.toJSON(param) },
        sortName: 'Id',
        sortOrder: 'desc',
        idField: 'Id',
        frozenColumns: [[{ title: '编号', field: 'Id', width: 80, sortable: true}]],
        columns: [[
                            { title: '字典类型', field: 'TypeId', width: 80 },
                            { title: '字典键名称', field: 'ItemKey', width: 80 },
                            { title: '字典键值', field: 'ItemValue', width: 80 },
                            { title: '父级编号', field: 'ParentId', width: 80 },
                            { title: '字段描述', field: 'Description', width: 150 },
                            { title: '排序编号', field: 'OrderId', width: 80 }
                   ]],
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: [
                    {
                        text: '添加',
                        iconCls: 'icon-add',
                        handler: Add
                    }, '-',
                    {
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: Update
                    }, '-',
                    {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: Del
                    }, '-',
                    {
                        text: '查询',
                        iconCls: 'icon-search',
                        handler: Search
                    }, '-',
                    {
                        text: '类型编辑',
                        iconCls: "icon-edit",
                        handler: function() {
                            $('#DictionaryType').window("open");
                        }
                    }
            ]
    });

}
var operate;
var row;
var window_edit;
var window_edit_form;
var grid;
var window_type;
var cmd_type;
var cmd_parent;
var param = { AssemblyName: "OA.Bll", ClassName: "OA.Bll.DictionaryInfoPresenter", Method: "", Options: {} };

function saveData() {
    if (!window_edit_form.form("validate") && operate != "search") return;
    var items = window_edit_form.serializeArray();
    $.each(items, function(i, item) {
        param.Options[item.name] = item.value;
    });
    if (operate == "add")
        param.Method = "Add";
    else if (operate == "update") {
        param.Method = "Update";
        param.Options["Id"] = row.Id;
    } else {
        window_edit.window("close");
        param.Method = "Search";
        grid.datagrid({ method: "post", queryParams: { "json": $.toJSON(param) }, url: "../Handler/AjaxHandle.ashx" });
        return;
    }

    $.ajax({
        type: "post",
        url: "../Handler/AjaxHandle.ashx",
        data: $.toJSON(param),
        dataType: "json",
        success: function(data) {
            if (data.flag) {
                grid.datagrid('reload');
                $.messager.show({ msg: data.msg, title: "info" });
                window_edit.window('close');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}
function Add() {
    window_edit.window({ title: '添加字典类型信息', iconCls: 'icon-add', closed: false });
    window_edit_form.form("clear");
    operate = "add";
}
function Update() {
    row = JSHelper.isDatagridSelected($("#Grid"));
    if (row == null) return;
    $('#TypeId').combobox("select", row.TypeId);
    $('#ItemKey').val(row.ItemKey);
    $('#ItemValue').val(row.ItemValue);
    $('#Description').val(row.Description);
    $("#ParentId").combobox("select", row.ParentId);
    $('#OrderId').val(row.OrderId);
    operate = "update";
    window_edit.window({ title: '修改字典类型信息', iconCls: 'icon-edit', closed: false });
}

function Del() {
    row = JSHelper.isDatagridSelected($("#Grid"));
    if (row == null) return;
    $.messager.confirm("删除提示", "是否确认要删除该条信息?", function(b) {
        if (b) {
            param.Method = "Delete";
            param.Options["Id"] = row.Id;
            $.ajax({
                type: "post",
                url: "../Handler/AjaxHandle.ashx",
                data: $.toJSON(param),
                dataType: "json",
                success: function(data) {
                    if (data.flag) {
                        grid.datagrid('reload');
                        $.messager.alert('提示', data.msg, 'info');
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            });
        }
    });


}
function Search() {
    window_edit_form.form("clear");
    operate = "search";
    window_edit.window({ title: '查询字典类型信息', iconCls: 'icon-search', closed: false });
}
